﻿#include "PadButton.h"

#include <QSoundEffect>
#include <QFileDialog>
#include <QMouseEvent>

PadButton::PadButton(QWidget* parent): QPushButton(parent)
{
    this->setFixedSize(100,100);
    QSizePolicy sizePo(QSizePolicy::Fixed,QSizePolicy::Fixed);
    this->setSizePolicy(sizePo);
    this->setStyleSheet("background-color:yellow");

    _sound = new QSoundEffect(this);
    _isContinue=false;
}

PadButton::~PadButton(){if(_sound)delete _sound;}

void PadButton::setPath()
{
    _sound->setSource(QFileDialog::getOpenFileUrl(this,"file",QUrl("D:\\"),"*.wav"));
}

void PadButton::play()
{
    if(_isContinue){
        if(!_sound->isPlaying()){
            _sound->play();
            qDebug("now playing");
            this->setStyleSheet("background-color:lime");
        }else{

        }
    }
    else
        if(_sound->isPlaying()){
            qDebug("stoped");
            stop();
        }
}

void PadButton::stop()
{
    _sound->stop();
    this->setStyleSheet("background-color:red");
}

void PadButton::changeContinue()
{
    if(_sound->isPlaying()){
        _isContinue=false;
        qDebug("wait stop");
        this->setStyleSheet("background-color:orange");
    }
    else{
        _isContinue=true;
        qDebug("wait start");
        this->setStyleSheet("background-color:green");
    }
}

void PadButton::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton){
        if(!_sound->source().isEmpty()) changeContinue();
    }else if(event->button()==Qt::RightButton){
        setPath();
        this->setStyleSheet("background-color:pink");
    }
}

